﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace PhoneAppCheckSensors
{
    using Microsoft.Devices.Sensors;

    public partial class PageMotionVisual : PhoneApplicationPage
    {
        public PageMotionVisual()
        {
            InitializeComponent();
        }

        private Motion mySensor;

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            mySensor=new Motion();
            mySensor.CurrentValueChanged+=new EventHandler<SensorReadingEventArgs<MotionReading>>(mySensor_CurrentValueChanged);
            mySensor.Start();

            base.OnNavigatedTo(e);
        }

        void mySensor_CurrentValueChanged(object sender, SensorReadingEventArgs<MotionReading> e)
        {
            Deployment.Current.Dispatcher.BeginInvoke(delegate()
            {
                double angleUnit = 45 / System.Math.PI;

                Storyboard sb = new Storyboard();
                double yaw = e.SensorReading.Attitude.Yaw;
                tbYaw.Text = String.Format("{0:0.000}", yaw);

                Duration duration = new Duration(TimeSpan.FromMilliseconds(500));
                sb.Duration = duration;

                DoubleAnimation yAnimation = new DoubleAnimation();
                yAnimation.Duration = duration;
                yAnimation.To = yaw * angleUnit;

              //  rotationYaw.Angle = yaw * angleUnit;

                double pitch = e.SensorReading.Attitude.Pitch;
                tbPitch.Text = String.Format("{0:0.000}", pitch);

                DoubleAnimation pAnimation = new DoubleAnimation();
                pAnimation.Duration = duration;
                pAnimation.To = pitch * angleUnit;
//                rotationPitch.Angle = pitch * angleUnit;

                double roll = e.SensorReading.Attitude.Roll;
                tbRoll.Text = String.Format("{0:0.000}", roll);

                DoubleAnimation rAnimation = new DoubleAnimation();
                rAnimation.Duration = duration;
                rAnimation.To = roll * angleUnit;
         //       rotationRoll.Angle = roll * angleUnit;

                sb.Children.Add(yAnimation);
                sb.Children.Add(pAnimation);
                sb.Children.Add(rAnimation);

                Storyboard.SetTarget(yAnimation, rotationYaw);
                Storyboard.SetTarget(pAnimation, rotationPitch);
                Storyboard.SetTarget(rAnimation, rotationRoll);
                Storyboard.SetTargetProperty(yAnimation, new PropertyPath(RotateTransform.AngleProperty));
                Storyboard.SetTargetProperty(pAnimation, new PropertyPath(RotateTransform.AngleProperty));
                Storyboard.SetTargetProperty(rAnimation, new PropertyPath(RotateTransform.AngleProperty));

                sb.Begin();
            });
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            mySensor.Stop();
            mySensor.Dispose();

            base.OnNavigatedFrom(e);
        }
    }
}